package Servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;

import DAL.Entites.Game;
import DAL.Entites.GameImage;
import DAL.Entites.Rank;
import DAL.JDBC.DBConn;
import DAL.JDBC.GameImageJDBC;
import DAL.JDBC.GameJDBC;
import DAL.JDBC.RankJDBC;

public class GetGames extends HttpServlet {
	private static final long serialVersionUID = 1L;
	RankJDBC rankJDBC;    

	public void init()
	{
		rankJDBC = new RankJDBC(DBConn.getConnection());
	}
    public GetGames() {
        super();
    }

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		JSONObject result;
		Map<String, String[]> params = request.getParameterMap();
		
		//checks if the request is for single game or for list of games
		if(params.containsKey("gameId"))
		{
			result = handleSingleGameRequest(request);
		}
		else
		{
			result = handleListOfGamesRequest(request);
		}
		
		PrintWriter pw = response.getWriter();
		pw.write(result.toString());	
	}
	
	private  JSONObject handleSingleGameRequest(HttpServletRequest request)
	{
		JSONObject result = new JSONObject();
		GameJDBC gameJDBC = new GameJDBC(DBConn.getConnection());
		GameImageJDBC gameImageJDBC = new GameImageJDBC(DBConn.getConnection());
		
		int gameId = Integer.parseInt(request.getParameter("gameId"));
		
		Game game = gameJDBC.getGameById(gameId);
		for (GameImage image : gameImageJDBC.listImagesByGameId(game.getId())) {
			game.addImage(image.getImagePath());
		}
		
		game.setRank(calcAverage(game.getId()));
		
		try {
			
			result.put("games", new JSONObject(game));
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		return result;
	}
	
	private JSONObject handleListOfGamesRequest(HttpServletRequest request)
	{
		JSONObject result = new JSONObject();
		GameJDBC gameJDBC = new GameJDBC(DBConn.getConnection());
		GameImageJDBC gameImageJDBC = new GameImageJDBC(DBConn.getConnection());
		
		int genreId = Integer.parseInt(request.getParameter("genreId"));
		int consoleId = Integer.parseInt(request.getParameter("consoleId"));
		
		List<Game> games = gameJDBC.listGamesByCriteria(consoleId, genreId);
		
		for (Game game : games) {
			GameImage firstImage = gameImageJDBC.listImagesByGameId(game.getId()).get(0);
			game.addImage(firstImage.getImagePath());
			game.setRank(calcAverage(game.getId()));
		}
		
		
		try {
			result.put("games", games);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		return result;
	}
	
	private float calcAverage(int GameId)
	{
		int total = 0;
		List<Rank> ranks = rankJDBC.listRanksByGameId(GameId);
		if(ranks.size() == 0)
		{
			return 0;
		}
		
		for (Rank rank : ranks) {
			total += rank.getRankNumber();
		}
		
		return total / ranks.size();
	}

}
